package com.usian.controller;

import com.usian.mapper.KeMapper;
import com.usian.mapper.XueMapper;
import com.usian.pojo.Ke;
import com.usian.pojo.Xue;
import net.minidev.json.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("/ke")
public class keController {
            @Autowired
            private KeMapper keMapper;
            @Autowired
                private XueMapper xueMapper;
  @RequestMapping("/ch")
    public  void daochu(HttpServletRequest request, HttpServletResponse response){
        JSONObject result=new JSONObject();
        try {
            //准备数据
            List<Ke> list = keMapper.selectList(null);
            List<Xue> list2 = xueMapper.selectList(null);

            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet("课程");
            HSSFSheet sheet2 = workbook.createSheet("学生");
            //课程
            HSSFRow row = sheet.createRow(0);
            int num = 0;
            //创建第一行第一个单元格
            HSSFCell cell = row.createCell(num++);
            cell.setCellValue("编号");
            cell = row.createCell(num++);
            cell.setCellValue("课程");
            cell = row.createCell(num++);
            cell.setCellValue("数量");

            for (int i = 0; i < list.size(); i++) {
                Ke empVo = list.get(i);
                row = sheet.createRow(sheet.getLastRowNum()+1);
                num = 0;
                //创建第一行第一个单元格
                cell = row.createCell(num++);
                cell.setCellValue(empVo.getId());
                cell = row.createCell(num++);
                cell.setCellValue(empVo.getName());
                cell = row.createCell(num++);
                cell.setCellValue(empVo.getShu());
            }
            //学生
            HSSFRow row2 = sheet2.createRow(0);
            int num2 = 0;
            //创建第一行第一个单元格
            HSSFCell cell2 = row2.createCell(num2++);
            cell2.setCellValue("编号");
            cell2 = row2.createCell(num2++);
            cell2.setCellValue("姓名");
            cell2 = row2.createCell(num2++);
            cell2.setCellValue("年龄");
            cell2 = row2.createCell(num2++);
            cell2.setCellValue("性别");
            for (int i = 0; i < list2.size(); i++) {
                Xue empVo2 = list2.get(i);
                row = sheet2.createRow(sheet2.getLastRowNum()+1);
                num = 0;
                //创建第一行第一个单元格
                cell2 = row.createCell(num++);
                cell2.setCellValue(empVo2.getId());
                cell2 = row.createCell(num++);
                cell2.setCellValue(empVo2.getName());
                cell2 = row.createCell(num++);
                cell2.setCellValue(empVo2.getAge());
                cell2 = row.createCell(num++);
                cell2.setCellValue(empVo2.getSex());
            }
            //下载文件
            response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode("员工.xls", "UTF-8"));
            response.setHeader("Connection", "close");
            response.setHeader("Content-Type", "application/octet-stream");
            workbook.write(response.getOutputStream());
            workbook.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("/chu")
    public  String chu2(){
      return "churu";
    }
}
